package hms.ReservationsModule;

import hms.database.DAO;
import hms.database.DAOnoGUI;
import hms.generic.AbstractGlobalView;

import java.awt.event.ActionEvent;
import java.sql.ResultSet;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*This class is used to control overall functions of the Guest Information sub-module.
 * will allow user to perform the following functions:
 * 	-create/update/delete guest information
 *  -display guest information
 *  -add/delete guests from database
 *   
 *@author Edgar Onukwugha*/
public class GuestInfoView extends AbstractGlobalView{
	
	protected JPanel guestInfo;
	protected JLabel name, address, gender, city, zipCode, phoneNumber, dateOfBirth;
	protected JButton addGuest, deleteGuest, manageInfo;
	JTextField firstNameField, lastNameField;
	JLabel firstNameLabel, lastNameLabel;
	
	GuestManagementView gmv = new GuestManagementView();
	
	DAO test = DAOnoGUI.getDAO();
	
	//initializes Guest Information sub-module
	//JLabels will be used to display guest's information called from 
	// database
	public JPanel initGuestInfoView(){
		guestInfo = new JPanel();
		guestInfo.setLayout(new BoxLayout(guestInfo, BoxLayout.Y_AXIS));
		
		//SELECT queries
		String selectName = "SELECT CONCAT(FIRST_NAME ' ' LAST_NAME) AS GUEST_NAME FROM GUEST";
		String selectAddress = "SELECT ADDRESS FROM GUEST";
		String selectGender = "SELECT GENDER FROM GUEST";
		String selectZip = "SELECT ZIP FROM GUEST";
		String selectHomePhone = "SELECT HOME_PHONE FROM GUEST";
		String selectWorkPhone = "SELECT WORK_PHONE FROM GUEST";
		String selectMobilePhone = "SELECT MOBILE_PHONE FROM GUEST";
		String selectDateOfBirth = "SELECT DATE_OF_BIRTH FROM GUEST";
		
		//ResultSet results
		/*
		String displayName = getResult(selectName);
		String displayAddress = ResultSet.getResult(selectAddress);
		String displayGender = ResultSet.getResult(selectGender);
		String displayZip = ResultSet.getResult(selectZip);
		String displayHomePhone = ResultSet.getResult(selectHomePhone);
		String displayWorkPhone = ResultSet.getResult(selectWorkPhone);
		String displayMobilePhone = ResultSet.getResult(selectMobilePhone);
		String displayDateOfBirth = ResultSet.getResult(selectDateOfBirth);*/
		
		
		//will display all attributes of guest's information
		name = new JLabel("Name: " );
		address = new JLabel("Address: " ); 
		gender = new JLabel("Gender: ");
		city = new JLabel("City: "); 
		zipCode = new JLabel("Zip: ");
		phoneNumber = new JLabel("Phone Number: "); 
		dateOfBirth = new JLabel("Date of Birth: ");
		
		//add labels
		JPanel infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
		infoPanel.add(name);
		infoPanel.add(address);
		infoPanel.add(gender);
		infoPanel.add(city);
		infoPanel.add(zipCode);
		infoPanel.add(phoneNumber);
		infoPanel.add(dateOfBirth);
		
		//initializes and sets actions to buttons
		addGuest = new JButton("Add Guest");
		addGuest.setActionCommand("addGuest");
		addGuest.addActionListener(this);
		deleteGuest = new JButton("Delete Guest");
		deleteGuest.setActionCommand("deleteGuest");
		deleteGuest.addActionListener(this);
		manageInfo = new JButton("Manage Guest Information");
		manageInfo.setActionCommand("manageInfo");
		manageInfo.addActionListener(this);
		
		//creates and adds buttons to button panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(addGuest);
		buttonPanel.add(deleteGuest);
		buttonPanel.add(manageInfo);
		
		//adds elements to GuestInfoView
		guestInfo.add(infoPanel);
		guestInfo.add(buttonPanel);
		
		return guestInfo;
	}
	
	/*sets all action events for JComponents used in GISM*/
	public void actionPerformed(ActionEvent e){
		
		/*action events for when "Add Button" is clicked*/
		if(e.getActionCommand().equalsIgnoreCase("addGuest")){
			
			firstNameLabel = new JLabel("First Name");
			lastNameLabel = new JLabel("Last Name");
			
			firstNameField = new JTextField(30);
			lastNameField = new JTextField(30);
						
			//sets components visible in option pane
			Object[] componentsArray = { 
					firstNameLabel, 
					firstNameField,
					lastNameLabel,
					lastNameField};
			
			//sets options for option pane 
			Object[] options = {"Add", "Cancel"};
			
			int response = JOptionPane.showOptionDialog(null, 
					componentsArray, 
					"Add Guest",
					JOptionPane.OK_CANCEL_OPTION,
	                JOptionPane.PLAIN_MESSAGE,
	                null,
	                options,
	                componentsArray[1]);	
			
			//if user presses "Add", add guest's name to guestNames vector list
			if (response == JOptionPane.YES_OPTION){
				
				String firstName = firstNameField.getText();
				String lastName = lastNameField.getText();
				
				//INSERT queries
				String insertGuestName = "INSERT INTO GUEST (FIRST_NAME, LAST_NAME) " +
						"VALUES (" + firstName + ", " + lastName + ")";				
				
				/*database interaction*/
				
				/*run queries here*/
				//used to test result; not necessary
				ResultSet insertResult = test.getResult(insertGuestName);
				test.updateCommand(insertGuestName);
				
				//close database
				
			}
		}
		
		/*action events for when "Delete Guest is pressed"*/
		if(e.getActionCommand().equalsIgnoreCase("deleteGuest")){
			
			int response = JOptionPane.showConfirmDialog(null, 
					"Are you sure you want to delete this guest?", 
					"Delete Guest",
					JOptionPane.OK_CANCEL_OPTION,
	                JOptionPane.PLAIN_MESSAGE,
	                null);
			
			//if user clicks "Yes", delete guest from guestNames 
			if (response == JOptionPane.YES_OPTION){
				String firstName = firstNameField.getText();
				String lastName = lastNameField.getText();
				
				//DELETE queries
				String deleteGuestInfo = "DELETE FROM GUEST " +
						"WHERE FIRST_NAME "+firstName+"= AND LAST_NAME = " + lastName;
				
				/*database interaction*/				
				
				/*run queries here*/
				//used to test result; not necessary
				ResultSet deleteGuest = test.getResult(deleteGuestInfo);
				
				test.updateCommand(deleteGuestInfo);
				
				//close database
				
			}
		}
		
		/*if Manage Information button is pressed, will call method from 
		 * GuestManagementView*/
		if(e.getActionCommand().equalsIgnoreCase("manageInfo")){
			//opens JOptionPane with editable components
			//called via GuestManagementView object

			Object[] manageChoices = {"Submit Changes", "Cancel"}; 
			
			int manageInfoDialog = JOptionPane.showOptionDialog(null,
					gmv.initGuestManagementView(),
					"Guest Management",
					JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.PLAIN_MESSAGE,
					null,
					manageChoices,
					null);
			if(manageInfoDialog == JOptionPane.YES_OPTION){
				
				//SELECT queries
				String selectGuestInfo = "SELECT * FROM GUEST" +
						"WHERE FIRST_NAME = "+" AND LAST_NAME = "+"";
				
				//UPDATE queries
				String updateAllInfo = "UPDATE GUEST SET "+
				"WHERE FIRST_NAME ="+" AND LAST_NAME ="+"";
				
				//INSERT queries
				String insertAllInfo = "INSERT INTO ";
				/*database interactions*/			
				//runs queries
				test.updateCommand(updateAllInfo);
				//if guest has no other information other than 
				test.updateCommand(insertAllInfo);
				
				//closes database
				
			}
		}
	}
}
